<template>
	<view>
		<view class="form_list">
			<scanCode :scanType="0"></scanCode>
			<!-- <view class="input2">
				<scanCode :scanType="0"></scanCode>
				<uni-easyinput prefixIcon="scan" disabled :placeholderStyle="placeholderStyle" 
				placeholder="请扫物料、库区二维码"></uni-easyinput>
			</view> -->
		</view>
		<view class="form_list">
			<view class="label">物料编码</view>
			<view class="input2" >
				<text>{{localInventory.number}}</text>
			</view>
		</view>
		<view class="form_list">
			<view class="label">基本单位</view>
			<view class="input2" style="width: 200rpx;">
				<text>{{localInventory.unit}}</text>
			</view>
			<view class="label">常用单位</view>
			<view class="input2" style="width: 290rpx;">
				<text>{{localInventory.store_unit}}</text>
			</view>
		</view>
		<view class="form_list">
			<view class="label">物料名称</view>
			<view class="input2">
				<text>{{localInventory.name}}</text>
			</view>
		</view>
		<view class="form_list">
			<view class="label">规格</view>
			<view class="input2">
				<text>{{localInventory.specification}}</text>
			</view>
		</view>
		
		<view class="form_list" v-show="(order_type==13||order_type==16)?false:true">
			<view class="label">建议库区</view>
			<view class="input2">
				<text>{{localInventory.tj_shelves_name}}</text>
			</view>
		</view>
		<!-- <view class="form_list" v-show="localInventory.lastest_shelves_name">
			<view class="label">最新库区</view>
			<view class="input2">
				<text>{{localInventory.lastest_shelves_name}}</text>
			</view>
		</view> -->
		<view class="form_list">
			<view class="label">库区</view>
			<view class="input3">
				<text>{{localShelves.name}}</text>
				<uni-icons custom-prefix="custom-icon" type="close" size="30" @click="clearShelves('from')" v-if="localShelves_icon"></uni-icons>
			</view>
		</view>
		<view class="form_list" v-show="getkcFlag">
			<view class="label">{{order_type==13?'账面数量':'库存'}}</view>
			<view class="input2">
				<text>{{fqty}}</text>
			</view>
		</view>
		<view class="form_list" v-show="flag=='in'?true:false">
			<view class="label">批号</view>
			<view class="input3">
				<uni-easyinput trim="all" type="text" v-model="cBatch" :placeholderStyle="placeholderStyle" :inputBorder="false" ></uni-easyinput>
			</view>
		</view>
		<view class="form_list" v-show="flag!='in'?true:false">
			<view class="label">批号</view>
			<view class="input2">
				<text>{{localInventory.cBatch}}</text>
				<uni-icons type="plusempty" :size="26" color="red" @click="toselect('batch')"></uni-icons>
			</view>
		</view>
		
		<inputNum :real_store_quantity.sync="real_store_quantity" :real_quantity.sync="real_quantity" :coefficient="localInventory.fCoefficient"></inputNum>
		<!-- <view class="form_list">
			<view class="label">基本数量</view>
			<view class="input4">
				<input type="number" v-model="real_quantity" disabled />
			</view>
		</view>
		<view class="form_list">
			<view class="label">常用数量</view>
			<view class="input3">
				<uni-easyinput  trim="all" type="number" v-model="real_aux_quantity" :placeholderStyle="placeholderStyle" :inputBorder="false" @change="inputQty" @focus="toFocus" @blur="toBlur" ></uni-easyinput>
			</view>
		</view> -->
		<view class="form_list" v-if="order_type==19?true:false">
			<view class="label">类型</view>
			<view class="input2">
				<uni-data-checkbox style="padding:16rpx" v-model="ifzz" :localdata="ifzz_arr"></uni-data-checkbox>
			</view>
		</view>
		<view class="form_list" v-if="false">  <!-- v-if="order_type==15?true:false" -->
			<view class="label">退回类型</view>
			<view class="input2">
				<uni-data-checkbox style="padding:16rpx" v-model="isnew" :localdata="isnew_arr"></uni-data-checkbox>
			</view>
		</view>
		<view class="form_list" v-if="allotFlag">
			<view class="label">目标库区</view>
			<view class="input3">
				<text>{{localToShelves.name}}</text>
				<uni-icons custom-prefix="custom-icon" type="close" size="30" @click="clearShelves('to')"></uni-icons>
			</view>
		</view>
		<view class="btn2" v-if="!allotFlag">
			<button @click="toselect('kc')" v-if="getkcFlag">库存 >></button>
			<button @click="toselect('inventory')">物料 >></button>
			<button @click="toselect('shelves')">库区 >></button>
		</view>
		<view class="btn2" v-if="allotFlag">
			<button @click="toselect('kc')" v-if="getkcFlag">库存 >></button>
			<button @click="toselect('shelves')">目标库区 >></button>
		</view>
	</view>
</template>

import { log } from "console";
<script>
	import inputNum from "@/component/input-num.vue"
	import {
		uniEasyinput,uniDataCheckbox
	} from '@dcloudio/uni-ui'
	export default {
		props: {
			getkcFlag:{
				type:Boolean,
				default:false
			},
			realQuantityZeroFlag:{//是否允许输入操作数量是0
				type:Boolean,
				default:true
			},
			kcQuantityZeroFlag:{//是否允许库存数量是0
				type:Boolean,
				default:true
			},
			stock_id:{
				type:Number,
				default:function(){
					return uni.getStorageSync('user').stock_id
				}
			},
			inventory: { //物料
			    type: Object,
			    default: function() {
				  return {};
				}
			},
			shelves:{//货架
				type:Object,
				default: function() {
				  return {};
				}
			},
			to_shelves:{//目标货架
				type:Object,
				default: function() {
				  return {};
				}
			},
			allotFlag:{
				type:Boolean,
				default:false
			},
			allot:{
				type:Object,
				default: function() {
				  return {};
				}
			},
			order_type:{
				//type:Number,
				default:0
			},
			toSuccessTip:{
				type:Boolean,
				default:true
			},
			flag:{//入库、出库动作标识 in out
				type:String,
				default:'out'
			}
		},
		components: {
			uniEasyinput,uniDataCheckbox,inputNum 
		},
		data() {
			return {
				scanItems:[],//明细
				placeholderStyle: 'font-size:17px;color:red',
				
				kc:{},//库存
				fqty:'',//库存
				real_quantity:0,//实际库存
				real_store_quantity:'0',//实际库存,常用数量
				cBatch:this.flag=='in'?this.$Func.getDateTime('today','Ymd'):'',//批号
				user:uni.getStorageSync('user'),
				
				localInventory:this.inventory,
				localShelves:this.shelves,
				localToShelves:{},
				localShelves_icon:true,
				
				isnew_arr: [{
					text: '新品',
					value: '新品'
				},
				{
					text: '良品',
					value: '良品'
				}],
				isnew:'新品',
				ifzz_arr: [{
					text: '子件',
					value: '子件'
				},
				{
					text: '母件',
					value: '母件'
				}],
				ifzz:'子件',
				select_stock_id:uni.getStorageSync('user').stock_id,
				select_to_stock_id:0
			}
		},
		computed: {},
		created() {
			//监听选中库存记录
			uni.$off("get_kc_sis")
			uni.$on("get_kc_sis", (item)=>{
				//单据类型17，选库存，有货架情况，不能重复上架
				if(this.order_type==17){
					if(!isNaN(item.shelves_id)&&item.shelves_id>0){
						//选择库存，但有货架，则不允许再上架
						this.$Api.msg('该库存已有货架，不能重复上架')
						return 
					}
				}
				//重新赋值
				item.fCoefficient = Number(item.fCoefficient) //转为数字
				this.localInventory = item
				this.kc = item
				
				//重新转换赋值，方便盘点用
				this.localInventory.number = item.cInvCode
				this.localInventory.kc_quantity = item.iQuantity
				this.localInventory.kc_id = item.id
				this.localInventory.unit_code = item.inv_unit_code
				
				this.kc_quantity = item.iQuantity
				this.fqty = item.iQuantity
				this.cBatch = item.cBatch
				
				if(this.allotFlag){
					//调拨的时候，才把库存数写入操作数
					this.real_quantity = Number(item.iQuantity)
					this.real_store_quantity = Number(item.iQuantity/item.fCoefficient).toString()
					
					//如果表头有设置默认库区，则表体默认库区
					this.localToShelves.id = this.allot.to_shelves_id
					this.localToShelves.name = this.allot.to_shelves_name
				}
				
				this.localShelves_icon = false //隐藏清除货架图标
				
				if(!isNaN(item.shelves_id)&&item.shelves_id>0){
					//库存有带货架信息的渲染
					this.localShelves.id = item.shelves_id
					this.localShelves.name = item.shelves_name
					this.localShelves.type = item.shelves_type
				}else{
					//没有则清空
					this.localShelves = {}
				}
			})
			
			//监听选中物料
			uni.$off("getMarr")
			uni.$on("getMarr", (item)=>{
				this.localInventory = item
				this.localInventory.fCoefficient = Number(item.fCoefficient)//转为数字
				
				this.localInventory.inv_id = item.id
				
				this.localShelves_icon = true //选择物料的，开放货架关闭
				this.clearShelves('from')//重扫物料 清空扫描货架信息
				if(this.flag == 'out'){
					this.kc = {} //清空库存对象
					this.cBatch = ''
				}
				
				//有传批号按批号，无则，入库相关，默认当天日期
				if(item.cBatch==undefined){
					if(this.flag == 'in'){
						this.cBatch = this.$Func.getDateTime('today','Ymd')
					}
				}else{
					this.cBatch = item.cBatch
				}
				
				//如果表头有设置默认库区，则表体默认库区
				if(this.allotFlag){
					this.localToShelves.id = this.allot.to_shelves_id
					this.localToShelves.name = this.allot.to_shelves_name
				}
				//this.getKc()
			})
			
			//监听选中货架
			uni.$off("get_Shelves")
			uni.$on("get_Shelves", (item)=>{
				/* if(this.allotFlag){
					//调拨逻辑处理
					this.localToShelves = item
				}else{
					this.localShelves = item
					this.getKc() 
				} */
				this.localShelves = item
				this.kc = {} //清空库存对象
				this.getKc()
			})
			
			//调拨逻辑处理 监听选中货架
			uni.$off("get_ToShelves")
			uni.$on("get_ToShelves", (item)=>{
				this.localToShelves = item
			})
			
			//表头选中仓库
			uni.$off("sel_stock_id")
			uni.$on("sel_stock_id", (sel_stock_id) => {
				this.select_stock_id = sel_stock_id
			})
			uni.$off("sel_to_stock_id")
			uni.$on("sel_to_stock_id", (sel_to_stock_id) => {
				this.select_to_stock_id = sel_to_stock_id
			})
		},
		destroyed() {
			uni.$off('get_kc_sis')
			uni.$off('getMarr')
			uni.$off('get_Shelves')
			uni.$off('sel_stock_id')
			uni.$off("sel_to_stock_id")
			uni.$off('get_ToShelves')
		},
		watch:{
			//监听父组件传递过来的，物料和货架
			shelves:{
				handler(newVal,oldVal){
					this.localShelves = newVal
					this.getKc()
				},
				deep: true
			},
			inventory: {
			    handler(newVal, oldVal) {
					this.localInventory = newVal
					// 处理对象变化的逻辑
					this.getKc()
			    },
			    deep: true
			}
		},
		methods: {
			async getKc(flag = false){//获得库存
				if(!this.getkcFlag&&!flag){//不检查库存
					return
				}
				
				if (JSON.stringify(this.localInventory)!='{}'&&JSON.stringify(this.localShelves)!='{}') {
					
					if(this.localInventory.iQuantity!=undefined){
						//从库存列表过来的,货架一致，不再查询即时库存
						if(this.localInventory.shelves_id==this.localShelves.id){
							this.localShelves_icon = false //选择库存的，隐藏选择库区
							return
						}
					}
					
					//单据类型17，选库存，空货架情况，则按直接上架处理，记录库存id,不再查库存
					if(this.order_type==17){
						if(this.localInventory.AutoID!=undefined){
							if(isNaN(this.kc.shelves_id)||this.kc.shelves_id==0){
								return 
							}
						}
					}
					
					uni.showLoading({
						mask:true
					})
					
					await this.$Db.getKc(this.localInventory.number,this.select_stock_id,this.localShelves.id,this.cBatch).then(res=>{
						if(res.length>0&&res[0].fqty!=undefined){
							this.localInventory.kc_quantity = parseFloat(res[0].fqty) //账面数量
							this.localInventory.kc_id = res[0].id //库存pc端id
							this.fqty = this.localInventory.kc_quantity
							
							//盘点的时候，才把库存数写入操作数
							if(this.allotFlag){
								this.real_quantity = this.fqty
							}
							this.localShelves_icon  = false //如果找到库存记录，隐藏本地选择货架按钮
						}else{
							if(!res.length){
								this.fqty = 0 
								this.real_quantity = 0
								this.real_store_quantity = '0'
							}
							this.localShelves_icon = true //如果找不到库存记录，显示本地选择货架按钮
						}
						uni.hideLoading()
					}).catch(err=>{
						this.$Api.msg(err.message)
						uni.hideLoading()
					})
				}
			},
			//选择弹窗
			toselect(name) {
				let url = ''
				if(name == 'kc'){
					this.globalData.toSecPage = 'kc' //进入二级页
					url = '/pages/kc/index?flag=checksis&stock_id='+this.select_stock_id
				}
				if(name == 'inventory'){
					this.globalData.toSecPage = 'inventory' //进入二级页
					url = '/pages/inventory/index?flag=check'
				}
				if(name == 'shelves'){
					this.globalData.toSecPage = 'shelves' //进入二级页
					if(this.allotFlag){
						if( this.localShelves.id == undefined){
							this.$Api.msg('原库区不能为空')
							return
						}
						url = '/pages/shelves/index?flag=allot&stock_id='+this.allot.to_stock_id
					}else{
						let select_stock_id = this.select_stock_id
						if(this.ifzz=='母件'){
							select_stock_id = this.select_to_stock_id
						}
						url = '/pages/shelves/index?flag=check&stock_id='+select_stock_id
					}
				}
				if(name == 'batch'){
					this.globalData.toSecPage = 'kc' //进入二级页
					if(this.localInventory.number == undefined){
						this.$Api.msg('请先选择物料')
						return
					}
					/* if(this.localShelves.id == undefined){
						this.$Api.msg('请先选择货架、库区')
						return
					} */
					url = '/pages/kc/index?flag=checksis&stock_id='+this.select_stock_id+'&inv_id='+this.localInventory.inv_id //+'&shelves_id='+this.localShelves.id
				}
				
				uni.showLoading({mask:true})
				uni.navigateTo({
					url:url,
					delta:1,
					success() {
						uni.hideLoading()
					}
				})
			},
			//核实添加物料
			async toAdd() {
				if (JSON.stringify(this.localInventory) == '{}'){
					this.$Api.msg('请扫物料二维码')
					return
				}
				if (JSON.stringify(this.localShelves) == '{}'){
					this.$Api.msg('请扫库区二维码')
					return
				}
				let addFlag = true
				
				//配置操作数据量不允许0
				if(!this.realQuantityZeroFlag){
					if(this.real_quantity == 0 || isNaN(this.real_quantity)){
						this.$Api.msg('操作数量不能为0')
						return
					}
				}else{
					if(isNaN(this.real_quantity)||this.real_quantity==''){
						this.real_quantity = 0
					}
				}
				
				//要获取库存&&配置库存kcQuantityZeroFlag不允许0
				if(this.getkcFlag&&!this.kcQuantityZeroFlag){
					if(this.fqty == 0 || isNaN(this.fqty)){
						this.$Api.msg('库存为0')//库存不能为空、0
						return
					}
				}else{
					if(isNaN(this.fqty)){
						this.fqty = 0
					}
				}
				
				//调拨需要的判断
				if(this.allotFlag){
					if (JSON.stringify(this.localToShelves) == '{}'){
						this.$Api.msg('请扫目标库区二维码')
						return
					}
					if(JSON.stringify(this.allot) == '{}'){
						this.$Api.msg('请选择调拨记录')
						return
					}
					if(this.allot.stock_id == this.allot.to_stock_id){
						//同库调拨
						if (this.localShelves.id==this.localToShelves.id){
							this.$Api.msg('原库区和目标库区相同')
							return
						}
					}else{
						//异库调拨、这里暂不进行，异库同货架判断 
					}
					
					//检查是否已存在调拨物料（同物料、同货架）
					let check = await this.scanItems.find(element => element.number == this.localInventory.number&&element.shelves_id==this.localShelves.id&&element.to_shelves_id==this.localToShelves.id&&element.cBatch==this.localInventory.cBatch)
					if(check!=undefined){
						this.$Api.msg(this.localInventory.name+'-'+this.localShelves.name+'-'+this.localToShelves.name+'-'+this.localInventory.cBatch+'已存在')
						return
					}
					
					if(this.fqty == 0 || isNaN(this.fqty)){
						this.$Api.msg('库存不能为0')
						return
					}
					
					if(this.real_quantity > this.fqty){
						this.$Api.msg('操作数量不能超过库存')
						return
					}
					
					this.localInventory.to_shelves_id = this.localToShelves.id
					this.localInventory.to_shelves_name = this.localToShelves.name
					
					try{
						//判断上架、出库策略
						//物料+目标货架，判断是否符合上架规格
						let temp_localInventory = this.localInventory
						temp_localInventory.shelves_id = this.localToShelves.id
						temp_localInventory.shelves_name = this.localToShelves.name
						temp_localInventory.shelves_type = this.localToShelves.type
						//await this.$Db.checkShangjiaJianhuo([temp_localInventory])
					}catch(e){
						//TODO handle the exception
						this.$Api.msg(e)
						return
					}
				}else{
					//扫描出现重复，直接覆盖数量 非调拨
					if(this.order_type==13){
						if(this.real_quantity==0&&this.real_quantity==this.fqty){
							this.$Api.msg('账面和盘点数不能都为0')
							return 
						}
						let check = await this.scanItems.map((element,index) => {
						  if(element.number == this.localInventory.number&&element.shelves_id==this.localShelves.id&&element.to_shelves_id==this.localToShelves.id&&element.cBatch==this.localInventory.cBatch){
							  element.real_quantity = this.real_quantity
							  element.real_store_quantity = this.real_quantity/this.localInventory.fCoefficient //根据基本数量*系数，得到常用数量
							  this.$set(this.scanItems,index,element)
							  addFlag = false
						  }
						  return element;
						});
					}else{
						//普通添加，就判断物料和货架是否重复就可
						let check = await this.scanItems.find(element => element.number == this.localInventory.number&&element.shelves_id==this.localShelves.id&&element.cBatch==this.localInventory.cBatch)
						if(check!=undefined){
							this.$Api.msg(this.localInventory.name+'-'+this.localShelves.name+'-'+this.localInventory.cBatch+'-已存在')
							return 
						}
					}
					//其他出库
					if(this.order_type == 12){
						if(parseFloat(this.fqty)>0 && parseFloat(this.real_quantity) > parseFloat(this.fqty)){
							this.$Api.msg('操作数量不能超过库存数量')
							return
						}
					}
				}
				
				this.localInventory.shelves_id = this.localShelves.id
				this.localInventory.shelves_name = this.localShelves.name
				this.localInventory.shelves_type = this.localShelves.type
				
				//判断上架、出库策略
				// try{
				// 	//涉及上架的单据id
				// 	if([0,1,11,15,17].includes(this.order_type)){
				// 		await this.$Db.checkShangjiaJianhuo([this.localInventory])
				// 	}
				// 	//涉及拣货出库
				// 	if([12,18].includes(this.order_type)){//14采购退回，没做判断
				// 		await this.$Db.checkShangjiaJianhuo([this.localInventory],'jianhuo')
				// 	}
				// }catch(e){
				// 	//TODO handle the exception
				// 	this.$Api.msg(e)
				// 	return
				// }
				
				this.localInventory.inv_name = this.localInventory.name
				this.localInventory.inv_number = this.localInventory.number
				this.localInventory.inv_specification = this.localInventory.specification
				this.localInventory.inv_unit = this.localInventory.unit
				this.localInventory.inv_unit_code = this.localInventory.unit_code
				this.localInventory.b_remark = '' //表体备注，默认空
				
				this.localInventory.real_quantity = this.real_quantity
				this.localInventory.real_store_quantity = this.real_quantity/this.localInventory.fCoefficient //根据基本数量*系数，得到常用数量
				
				this.localInventory.cBatch = this.cBatch
				
				//领料退货类型
				if(this.order_type == 15){
					this.localInventory.b_remark = this.isnew
				}
				//组装类型
				if(this.order_type == 19){
					this.localInventory.b_remark = this.ifzz
					
					await this.getKc(true)
					if(this.ifzz == '子件' && this.localShelves_icon){
						this.$Api.msg('库存不足,请核实')
						return 
					}
				}
				
				//检查是否启用批号，有启用批号
				if(this.localInventory.mbatch == 1){
					if(this.localInventory.cBatch==''||this.localInventory.cBatch==undefined){
						this.$Api.msg('该物料启用批号,批号不能为空')
						return 
					}
				}else{
					//未启用批号
					if(this.localInventory.cBatch!=''&&this.localInventory.cBatch!=undefined){
						this.$Api.msg('该物料未启用批号,请清空批号')
						return 
					}
				}
				//
				
				if(this.allotFlag){
					//调拨时要判断，物料分类策略
					let msg = ''
					await this.$Db.checkShelvesInvclass([this.localInventory],this.localInventory.to_shelves_id).catch(err=>{
						this.$Db.addlog('添加调拨',err)
						msg = err
					})
					if(msg){
						this.$Api.msg(msg)
						return
					}
				}
				
				if(addFlag){
					this.scanItems.push(this.localInventory)
				}
				
				if(this.allotFlag){
					uni.$emit('allotItems',this.scanItems,[this.localInventory])
				}else{
					uni.$emit('checkItems',this.scanItems,[this.localInventory])
				}
				
				this.reset()
				if(this.toSuccessTip){
					this.$Api.msg('添加成功','success')
				}
			},
			inputQty(val){
				//格式化数量,小数点后x位
				this.real_quantity = this.$Func.decimalPattern(val)
			},
			reset(clearScanItems = false) {
				this.localInventory = {}
				this.localShelves = {}
				this.localToShelves = {}
				this.kc = {}
				
				this.fqty = ''
				this.real_quantity = 0
				this.real_store_quantity = '0'
				this.ifzz = '子件'
				
				if(this.flag == 'out'){
					//出库才清
					this.cBatch = ''
				}
				
				this.localShelves_icon = true //恢复默认本地选择货架按钮
				
				if(clearScanItems){
					this.scanItems = [] //清空表体数组
				}
			},
			clearShelves(name){
				//手动改变货架就清空
				if(name == 'to'){
					this.localToShelves = {}
				}else{
					this.localShelves = {}
					//this.$set(this.localShelves,'name','')
					//this.$set(this.localShelves,'id','')
					this.fqty = ''
					this.real_quantity = 0
					this.real_store_quantity = '0'
				}
			},
			// toFocus(){
			// 	if(this.real_quantity == 0){
			// 		this.real_quantity = ''
			// 	}
			// },
			// toBlur(){
			// 	if(this.real_quantity == ''){
			// 		this.real_quantity = 0
			// 	}
			// }
		}
	}
</script>
<style lang="scss">
</style>